<template>
  <div>
    <el-form :model="formData" label-width="80px" ref="form" :rules="rules">
      <el-form-item label="商品名称" prop="goods_name">
        <el-input v-model="formData.goods_name" placeholder="请输入商品名称"></el-input>
      </el-form-item>
      <el-form-item label="商品价格" prop="goods_price">
        <el-input type="number" v-model="formData.goods_price" placeholder="请输入商品价格"></el-input>
      </el-form-item>
      <el-form-item label="商品主图" prop="images">
        <multiuploader v-model="formData.images" />
      </el-form-item>
      <el-form-item label="商品详情" prop="content">         
         <editor v-model="formData.content"/>
      </el-form-item>
      <el-form-item>  
        <el-button type="primary" @click="submit">提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {update} from "@/api/shop/goods"
import multiuploader from "@/components/Uploader/multiuploader"
import editor from "@/components/Editor/editor.vue"
  export default {
    components:{
      multiuploader,
      editor
    },
    props:[
      'row'
    ],
    watch:{
      row:{
        handler:function(val){
          console.log(val,'row')
          if(val){
            this.formData = val;
            if(typeof(val.images) == 'string' ){
              this.formData.images = JSON.parse(val.images)
            }
          }
        },
        immediate:true
      }
    },
    data(){
      return {
        formData:{
          id:'',
          goods_name:'',          
          goods_price:'',
          images:[],
          content:''
        },
        rules:{
          goods_name:[
            {required:true,message:'请输入商品名称'}
          ],
          goods_price:[
            {required:true,message:'请输入商品价格'}
          ],
          images:[
            {required:true,message:'请上传商品主图'}
          ],
          content:[
            {required:true,message:'请输入商品详情'}
          ]
        }
      }
    },
    methods:{
      submit(){
        this.$refs.form.validate(valid=>{
          if(valid){
            update(this.formData).then(res=>{
              this.$message.success("更新成功")
              this.$emit('success')
            })
          }          
        })
      }
    }
  }
</script>

<style scoped>

</style>